package com.sdap2.controller;

import com.sdap2.common.model.ApiResult;
import com.sdap2.entity.CameraData;
import com.sdap2.service.CameraService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Tag(name = "摄像头管理")
@RestController
@RequestMapping("/api/cameras")
@RequiredArgsConstructor
public class CameraController {
    
    private final CameraService cameraService;
    
    @Operation(summary = "摄像头抓拍")
    @PostMapping("/{deviceSerial}/capture")
    public ApiResult<String> capture(
            @Parameter(description = "设备序列号") @PathVariable String deviceSerial,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        String picUrl = cameraService.capture(deviceSerial, token);
        return ApiResult.success(picUrl);
    }
    
    @Operation(summary = "查询抓拍历史")
    @GetMapping("/{deviceSerial}/history")
    public ApiResult<Page<CameraData>> getHistory(
            @Parameter(description = "设备序列号") @PathVariable String deviceSerial,
            @Parameter(description = "分页参数") @PageableDefault(size = 10) Pageable pageable,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        return ApiResult.success(cameraService.getHistory(deviceSerial, pageable, token));
    }

    @Operation(summary = "查询指定时间范围的抓拍历史")
    @GetMapping("/{deviceSerial}/history/time-range")
    public ApiResult<Page<CameraData>> getHistoryByTimeRange(
            @Parameter(description = "设备序列号") @PathVariable String deviceSerial,
            @Parameter(description = "开始时间") @RequestParam LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam LocalDateTime endTime,
            @Parameter(description = "分页参数") @PageableDefault(size = 10) Pageable pageable,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        return ApiResult.success(cameraService.getHistory(deviceSerial, startTime, endTime, pageable, token));
    }

    @Operation(summary = "获取最新一次抓拍记录")
    @GetMapping("/{deviceSerial}/latest")
    public ApiResult<CameraData> getLatestCapture(
            @Parameter(description = "设备序列号") @PathVariable String deviceSerial,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        return ApiResult.success(cameraService.getLatestCapture(deviceSerial, token));
    }
} 